var area_picker = require("./../../components/area-picker/area-picker.js");

Page({
    data: {
        name: "",
        mobile: "",
        detail: "",
        // district: ['请选择', '请选择', '请选择'],
        province: '',
        city: '',
        district: '',
    },
    bindRegionChange(e) {
        console.log(e.detail.value)
        this.setData({
            province: e.detail.value[0],
            city: e.detail.value[1],
            district: e.detail.value[2]
        })
    },
    onLoad: function (e) {
        getApp().page.onLoad(this, e);
        var t = this;
        t.getDistrictData(function (e) {
            area_picker.init({
                page: t,
                data: e
            });
        }), t.setData({
            address_id: e.id
        }), e.id && (getApp().core.showLoading({
            title: "正在加载",
            mask: !0
        }), getApp().request({
            url: getApp().api.user.address_detail,
            data: {
                id: e.id
            },
            success: function (e) {
                getApp().core.hideLoading()
                if (e.code === 0) {
                    t.setData(e.data)
                    t.setData({
                        province: e.data.district.province.name,
                        district: e.data.district.district.name,
                        city: e.data.district.city.name,
                    })
                    console.log('data++++',t.data)
                }
            }
        }));
    },
    getDistrictData: function (t) {
        var i = getApp().core.getStorageSync(getApp().const.DISTRICT);
        if (!i) return getApp().core.showLoading({
            title: "正在加载",
            mask: !0
        }), void getApp().request({
            url: getApp().api.default.district,
            success: function (e) {
                console.log('1111111')
                getApp().core.hideLoading(), 0 == e.code && (i = e.data, getApp().core.setStorageSync(getApp().const.DISTRICT, i),
                    t(i));
            }
        });
        t(i);
    },
    onAreaPickerConfirm: function (e) {
        this.setData({
            district: {
                province: {
                    id: e[0].id,
                    name: e[0].name
                },
                city: {
                    id: e[1].id,
                    name: e[1].name
                },
                district: {
                    id: e[2].id,
                    name: e[2].name
                }
            }
        });
    },
    saveAddress: function () {
        var t = this;
        getApp().core.showLoading({
            title: "正在保存",
            mask: !0
        });
        var e = t.data.district;
        console.log(e)
        if (e[0] == '请选择') {
            wx.showToast({
                title: '请选择地址',
                icon: 'none'
            })
            return false
        }
        getApp().request({
            url: getApp().api.user.address_save,
            method: "post",
            data: {
                address_id: t.data.address_id || "",
                name: t.data.name,
                mobile: t.data.mobile,
                province_id: t.data.province,
                city_id: t.data.city,
                district_id: t.data.district,
                detail: t.data.detail
            },
            success: function (e) {
                getApp().core.hideLoading(), 0 == e.code && getApp().core.showModal({
                    title: "提示",
                    content: e.msg,
                    showCancel: !1,
                    success: function (r) {
                        r.confirm &&getApp().core.setStorageSync(getApp().const.PICKER_ADDRESS, e.data),wx.navigateBack({delta: 2});
                    }
                }), 1 == e.code && t.showToast({
                    title: e.msg
                });
            }
        });
    },
    inputBlur: function (e) {
        var t = '{"' + e.currentTarget.dataset.name + '":"' + e.detail.value + '"}';
        this.setData(JSON.parse(t));
    },
    getWechatAddress: function (e) {
        var i = this;

        getApp().core.chooseAddress({
            success: function (t) {
                "chooseAddress:ok" == t.errMsg && (
                    i.setData({
                        name: t.userName || "",
                        mobile: t.telNumber || "",
                        detail: t.detailInfo || "",
                        province:t.provinceName || "",
                        city: t.cityName || "",
                        district: t.countyName || "",
                    })
                );
            }
        });
    },
    onShow: function () {
        getApp().page.onShow(this);
    }
});